//
//  CYMainNavigationController.swift
//  MySina
//
//  Created by mac on 2017/1/16.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit

class CYMainNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 当自定义导航左侧按钮后 点击屏幕左侧不能返回的bug 解决方法
        interactivePopGestureRecognizer?.delegate = self
    }
    // 将要接受手势点击
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {

        return childViewControllers.count != 1
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 进入的二级控制器
        if childViewControllers.count > 0 {
            // 设置title
            var title = "返回"
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? ""
                // 隐藏
                viewController.hidesBottomBarWhenPushed = true
                
            }
            
            // 设置左侧按钮
            /*
             如果你自定义了左侧返回按钮后 那么点击屏幕左侧 进行拖拽就会失效
             */
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(leftButtonClick))
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    func leftButtonClick() {
        _ = popViewController(animated: true)
    }
}
